#include "atk_ms901m_uart.h"

static DL_UART_Main_Config g_uart_handle; /* ATK-MS901M UART */
static struct
{
    uint8_t buf[ATK_MS901M_UART_RX_FIFO_BUF_SIZE]; /* 缓冲 */
    uint16_t size;                                 /* 缓冲大小 */
    uint16_t reader;                               /* 读指针 */
    uint16_t writer;                               /* 写指针 */
} g_uart_rx_fifo;                                  /* UART接收FIFO */

/**
 * @brief       ATK-MS901M UART接收FIFO写入数据
 * @param       dat: 待写入数据
 *              len: 待写入数据的长度
 * @retval      0: 函数执行成功
 *              1: FIFO剩余空间不足
 */
uint8_t atk_ms901m_uart_rx_fifo_write(uint8_t *dat, uint16_t len)
{
    uint16_t i;

    /* 将数据写入FIFO
     * 并更新FIFO写入指针
     */
    for (i = 0; i < len; i++)
    {
        g_uart_rx_fifo.buf[g_uart_rx_fifo.writer] = dat[i];
        g_uart_rx_fifo.writer = (g_uart_rx_fifo.writer + 1) % g_uart_rx_fifo.size;
    }

    return 0;
}

/**
 * @brief       ATK-MS901M UART接收FIFO读取数据
 * @param       dat: 读取数据存放位置
 *              len: 欲读取数据的长度
 * @retval      0: FIFO中无数据
 *              其他值: 实际读取的数据长度
 */
uint16_t atk_ms901m_uart_rx_fifo_read(uint8_t *dat, uint16_t len)
{
    uint16_t fifo_usage;
    uint16_t i;

    /* 获取FIFO已使用大小 */
    if (g_uart_rx_fifo.writer >= g_uart_rx_fifo.reader)
    {
        fifo_usage = g_uart_rx_fifo.writer - g_uart_rx_fifo.reader;
    }
    else
    {
        fifo_usage = g_uart_rx_fifo.size - g_uart_rx_fifo.reader + g_uart_rx_fifo.writer;
    }

    /* FIFO数据量不足 */
    if (len > fifo_usage)
    {
        len = fifo_usage;
    }

    /* 从FIFO读取数据
     * 并更新FIFO读取指针
     */
    for (i = 0; i < len; i++)
    {
        dat[i] = g_uart_rx_fifo.buf[g_uart_rx_fifo.reader];
        g_uart_rx_fifo.reader = (g_uart_rx_fifo.reader + 1) % g_uart_rx_fifo.size;
    }

    return len;
}

/**
 * @brief       ATK-MS901M UART接收FIFO清空
 * @param       无
 * @retval      无
 */
void atk_ms901m_rx_fifo_flush(void)
{
    g_uart_rx_fifo.writer = g_uart_rx_fifo.reader;
}

/**
 * @brief       ATK-MS901M UART发送数据
 * @param       dat: 待发送的数据
 *              len: 待发送数据的长度
 * @retval      无
 */
void atk_ms901m_uart_send(uint8_t *dat, uint8_t len)
{
    // HAL_UART_Transmit(&g_uart_handle, dat, len, HAL_MAX_DELAY);
    // 当前字符串地址不在结尾 并且 字符串首地址不为空
    while (*dat != 0 && dat != 0)
    {
        // 发送字符串首地址中的字符，并且在发送完成之后首地址自增
        while (DL_UART_isBusy(UART_3_INST) == true)
            ;
        DL_UART_Main_transmitData(UART_3_INST, *dat++);
    }
}

/**
 * @brief       ATK-MS901M UART初始化
 * @param       baudrate: UART通讯波特率
 * @retval      无
 */
void atk_ms901m_uart_init()
{
    g_uart_rx_fifo.size = ATK_MS901M_UART_RX_FIFO_BUF_SIZE; /* UART接收FIFO缓冲大小 */
    g_uart_rx_fifo.reader = 0;                              /* UART接收FIFO读指针 */
    g_uart_rx_fifo.writer = 0;                              /* UART接收FIFO写指针 */
}

/**
 * @brief       ATK-MS901M UART中断回调函数
 * @param       无
 * @retval      无
 */
void UART_3_INST_IRQHandler(void)
{
    uint8_t tmp;
    switch (DL_UART_getPendingInterrupt(UART_3_INST))
    {
    case DL_UART_IIDX_OVERRUN_ERROR:
        DL_UART_getRawInterruptStatus(UART_3_INST, DL_UART_IIDX_OVERRUN_ERROR);
        DL_UART_Main_receiveData(UART_3_INST);
        break;
    case DL_UART_IIDX_RX: // 如果是接收中断
        // 接收发送过来的数据保存
        tmp = DL_UART_Main_receiveData(UART_3_INST);
        atk_ms901m_uart_rx_fifo_write(&tmp, 1);
        break;
    default: // 其他的串口中断
        break;
    }
    DL_UART_clearInterruptStatus(UART_3_INST, DL_UART_IIDX_OVERRUN_ERROR);
}
